<template>
    <view class="container">
        <view class="user-info">
            <image 
                :src="userInfo.avatarUrl" 
                class="avatar"
            ></image>
            <text class="username">{{ userInfo.nickName }}</text>
        </view>

        <view class="profile-stats">
            <view class="stat-item">
                <text class="stat-value">{{ totalRecords }}</text>
                <text class="stat-label">总记录</text>
            </view>
            <view class="stat-item">
                <text class="stat-value">{{ continuousDays }}</text>
                <text class="stat-label">连续记录</text>
            </view>
            <view class="stat-item">
                <text class="stat-value">{{ averageMoodScore }}</text>
                <text class="stat-label">平均心情</text>
            </view>
        </view>

        <view class="menu-section">
            <view 
                class="menu-item" 
                @tap="navigateTo('/pages/privacy/index')"
            >
                <text class="iconfont icon-privacy">&#xe606;</text>
                <text class="menu-text">隐私政策</text>
                <text class="iconfont icon-right">&#xe607;</text>
            </view>

            <view 
                class="menu-item" 
                @tap="navigateTo('/pages/feedback/index')"
            >
                <text class="iconfont icon-feedback">&#xe608;</text>
                <text class="menu-text">意见反馈</text>
                <text class="iconfont icon-right">&#xe607;</text>
            </view>

            <view 
                class="menu-item" 
                @tap="showAboutModal"
            >
                <text class="iconfont icon-about">&#xe609;</text>
                <text class="menu-text">关于我们</text>
                <text class="iconfont icon-right">&#xe607;</text>
            </view>
        </view>

        <view class="actions">
            <button 
                class="logout-btn" 
                @tap="confirmLogout"
            >
                退出登录
            </button>
        </view>
    </view>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
    data() {
        return {
            totalRecords: 0,
            continuousDays: 0,
            averageMoodScore: 0
        }
    },
    computed: {
        ...mapState('user', ['userInfo'])
    },
    onLoad() {
        this.fetchProfileStats();
    },
    methods: {
        ...mapActions('user', ['logout']),
        
        async fetchProfileStats() {
            try {
                const stats = await this.$api.user.getProfileStats();
                this.totalRecords = stats.totalRecords;
                this.continuousDays = stats.continuousDays;
                this.averageMoodScore = stats.averageMoodScore;
            } catch (error) {
                uni.showToast({
                    title: '获取统计信息失败',
                    icon: 'none'
                });
            }
        },
        navigateTo(url) {
            uni.navigateTo({ url });
        },
        showAboutModal() {
            uni.showModal({
                title: '关于心情盒子',
                content: '心情盒子是一款帮助您记录和分析个人情绪的小程序。我们致力于通过AI技术，帮助用户更好地了解自己的情绪变化。',
                showCancel: false,
                confirmText: '我知道了'
            });
        },
        confirmLogout() {
            uni.showModal({
                title: '退出登录',
                content: '确定要退出登录吗？',
                success: (res) => {
                    if (res.confirm) {
                        this.handleLogout();
                    }
                }
            });
        },
        handleLogout() {
            // 调用Vuex的logout方法
            this.logout();
            
            // 跳转到登录页
            uni.reLaunch({
                url: '/pages/home/login'
            });
        }
    }
}
</script>

<style scoped>
.container {
    background-color: #F8F8F8;
    height: 100vh;
    padding: 24px;
}

.user-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 24px;
}

.avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    margin-bottom: 16px;
}

.username {
    font-size: 18px;
    font-weight: 600;
    color: #333;
}

.profile-stats {
    display: flex;
    justify-content: space-around;
    background-color: white;
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 24px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-value {
    font-size: 20px;
    font-weight: 600;
    color: #1AAD19;
    margin-bottom: 8px;
}

.stat-label {
    color: #666;
    font-size: 14px;
}

.menu-section {
    background-color: white;
    border-radius: 12px;
    margin-bottom: 24px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.menu-item {
    display: flex;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid #F0F0F0;
}

.menu-item:last-child {
    border-bottom: none;
}

.menu-item .iconfont {
    font-size: 20px;
    margin-right: 16px;
}

.menu-item .icon-privacy { color: #1AAD19; }
.menu-item .icon-feedback { color: #1890FF; }
.menu-item .icon-about { color: #722ED1; }

.menu-text {
    flex-grow: 1;
    color: #333;
}

.menu-item .icon-right {
    color: #999;
}

.actions {
    margin-top: 24px;
}

.logout-btn {
    width: 100%;
    background-color: #FF4D4F;
    color: white;
    border: none;
    padding: 16px;
    border-radius: 32px;
}
</style> 